
package com.wzt.lianfirecontrol.utils;

import android.content.Context;
import android.text.format.DateFormat;

import com.wzt.lianfirecontrol.bean.ConstData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class TimeUtils {
    public static String dateToWeek(int position) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        Date currentDate = new Date();
        int b = currentDate.getDay();
        Date fdate;
        List<String> list = new ArrayList<String>();
        Long fTime = currentDate.getTime();
        for (int a = 0; a < 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(sdf.format(fdate));
        }
        return list.get(position);
    }

    /**
     * @param format
     * @return
     */
    public static String getCurrentTime(String format) {
        return getFormatDateTime(new Date(), format);
    }
    
    public static String getFormatDateTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        return sdf.format(date);
    }

    /**
     * 获取下个月时间
     * @param months
     * @return
     */
    public static String getNextMonth(Date date, int months){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ConstData.TIME_FORMAT_YYYY_MM_DD);
        Calendar calendar = simpleDateFormat.getCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, months * 30);
        String nextMonth = simpleDateFormat.format(calendar.getTime());
        return nextMonth;
    }

    public static String getLocalTime(Context context, String time) {
        // 取出年月日来，比较字符串即可
        String str_curTime = DateFormat.format("yyyy-MM-dd", new Date()).toString();
        int result = str_curTime.compareTo(time.substring(0, time.indexOf(" ")));
        if (result > 0) {
            String tomorow = time.substring(time.indexOf(" "), time.lastIndexOf(":"));
            return "昨天" + tomorow;
        } else if (result == 0) {
            String today = time.substring(time.indexOf(" "), time.lastIndexOf(":"));
            return "今天" + today;
        } else {
            return time;
        }
    }

    /**
     * 是否已过今天
     * @param time
     * @return
     */
    public static boolean isTimeOut(String time) {
        // 取出年月日来，比较字符串即可
        String str_curTime = DateFormat.format("yyyy-MM-dd", new Date()).toString();
        int result = str_curTime.compareTo(time);
        if (result > 0) {
            return true;
        }
        return false;
    }

    public static boolean isToday(String date) {
        String str_curTime = DateFormat.format("yyyy-MM-dd", new Date()).toString();
        int result = str_curTime.compareTo(date);
        if (result == 0) {
            return true;
        }

        return false;
    }

    public static boolean isSeveralDyasBefore(String date, int days) {
        Date d = new Date();
        String strTime = DateFormat.format("yyyy-MM-dd", new Date(d.getTime() - (days * 24 * 60 * 60 * 1000))).toString();
        int result = strTime.compareTo(date);
        if (result == 0) {
            return true;
        }

        return false;
    }

    public static boolean isThisYear(String year) {
        String str_curTime = DateFormat.format("yyyy", new Date()).toString();
        int result = str_curTime.compareTo(year);
        if (result == 0) {
            return true;
        }

        return false;
    }
    
    /**
     * 是否已过保存的天数
     * @param time 保存的时间
     * @param days 保存的天数
     * @return
     */
    public static boolean isTimeOut(String time, int days) {
        // 取出年月日来，比较字符串即可
        Date d = new Date();
        String strTime = DateFormat.format("yyyy-MM-dd", new Date(d.getTime() - (days * 24 * 60 * 60 * 1000))).toString();
        int result = strTime.compareTo(time);
        if (result > 0) {
            return true;
        }
        return false;
    }

    /**
     * 返回一个date的前dates天或者后dates天
     * @param date
     * @param dates
     * @return
     */
    public static Date getNextOrPreDay(Date date, int dates) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, dates);
        date = calendar.getTime();
        return date;
    }

    public static boolean checkDateStr(String oldDate) {
        if (StringUtils.isEmpty(oldDate)) {
            return false;
        }

        if (oldDate.length() != 19) {
            return false;
        }

        return true;
    }

    /**
     * 格式化时间格式yyyy-MM-dd HH:mm:ss按要求显示
     * @param oldDate
     * @return
     */
    public static String getShowDate(String oldDate) {
        if (!checkDateStr(oldDate)) {
            return "";
        }

        if (TimeUtils.isToday(oldDate.substring(0, 10))) {
            return oldDate.substring(11, 16);
        }

        if (TimeUtils.isThisYear(oldDate.substring(0, 4))) {
            return getMonthDay(oldDate);
        }

        return getYearMonthDay(oldDate);
    }

    public static String getMonthDay(String oldDate) {
        String month = oldDate.substring(5, 7);
        String day = oldDate.substring(8, 10);
        if (month.startsWith("0")) {
            month = oldDate.substring(6, 7);
        }
        if (day.startsWith("0")) {
            day = oldDate.substring(9, 10);
        }

        return month + "月" + day + "日";
    }

    public static String getYearMonthDay(String oldDate) {
        return oldDate.substring(0, 4) + getMonthDay(oldDate);
    }

    public static String getDayAllStr(String oldDate) {
        return oldDate.substring(0, 4) + "年" + getMonthDay(oldDate) + " " + oldDate.substring(11, 16);
    }

    /**
     * 判断是否在周末
     * @param date
     * @return
     */
    public static boolean isWeekend(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        }
        return false;
    }

    /**
     * 获取相差天数
     * @param date
     * @return
     */
    public static long getDifferenceDays(Date date){
        long dateDay = date.getTime() / 1000 / 60 / 60 / 24;
        long currentDay = ((new Date()).getTime() / 1000 / 60 / 60 + 8) / 24;
        return currentDay - dateDay;
    }

    /**
     * 获取相差小时
     * @param date
     * @return
     */
    public static long getDifferenceHours(Date date){
        long dateHours = date.getTime() / 1000 / 60 / 60;
        long currentHours = (new Date()).getTime() / 1000 / 60 / 60;
        return currentHours - dateHours;
    }
}
